<template>
  <div class="orderBox">
    <h2 class="pageTitle">填写订单信息</h2>
    <div class="infoBox">
      <!-- <div class="title">会议室租赁订单信息</div> -->
      <div class="infoTitle">
        <span>租赁公司</span>
      </div>
      <div class="infoDetail">
        <el-row :gutter="20">
          <el-col :span="10"
            ><div>公司名称：{{ companyData.companyName }}</div></el-col
          >
          <el-col :span="6"
            ><div>联系人：{{ companyData.linkMan }}</div></el-col
          >
          <el-col :span="8"
            ><div>联系方式：{{ companyData.linkPhone }}</div></el-col
          >
        </el-row>
        <el-row :gutter="20">
          <el-col :span="10"
            ><div>统一社会信用代码：{{ companyData.companyCode }}</div></el-col
          >
          <el-col :span="6"
            ><div>法人：{{ companyData.legalMan }}</div></el-col
          >
          <el-col :span="8"
            ><div>法人联系方式：{{ companyData.legalManPhone }}</div></el-col
          >
        </el-row>
      </div>
      <div class="infoTitle">
        <span>会议室基础信息</span>
      </div>
      <div class="infoDetail">
        <el-row :gutter="20">
          <el-col :span="10"
            ><div>位置：{{ adressData.address }}</div></el-col
          >
          <el-col :span="6"
            ><div>楼层：{{ adressData.floor }}层</div></el-col
          >
          <el-col :span="8"
            ><div>面积：{{ adressData.area }} ㎡</div></el-col
          >
        </el-row>
      </div>
      <el-form
        :model="ruleForm"
        status-icon
        ref="ruleForm"
        label-width="auto"
        class="demo-ruleForm"
      >
        <div class="infoTitle">
          <span>会议时间</span>
          <span class="rightTxt"
            >小计：<span class="redTxt">{{ dateCountPrice }}</span
            >元</span
          >
        </div>
        <div class="infoDetail">
          <el-row
            :gutter="20"
            v-for="(item, index) in ruleForm.dateTimeArr"
            :key="index"
          >
            <div>
              <el-col :span="8">
                <div>
                  <el-form-item label="会议日期">
                    <el-date-picker
                      type="date"
                      placeholder="选择日期"
                      v-model="item.meetingDate"
                      format="yyyy-MM-dd"
                      value-format="yyyy-MM-dd"
                      :picker-options="option"
                      @change="dateChange(item, index)"
                    ></el-date-picker>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="8">
                <div>
                  <el-form-item label="会议时段">
                    <el-select
                      v-model="item.meetingTimeFrame"
                      placeholder="请选择会议时段"
                      :disabled="item.meetingDate == ''"
                      @change="countDateInfo()"
                    >
                      <el-option
                        v-for="(it, index) in item.timeArr"
                        :key="index"
                        :label="it.label"
                        :value="it.value"
                        :disabled="it.disabled"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </div>
              </el-col>

              <el-col :span="8">
                <div v-if="index == 0">
                  <el-button
                    type="text"
                    icon="el-icon-plus"
                    @click="addDateTime()"
                    >增加会议时间</el-button
                  >
                </div>
                <div v-else style="line-height: 40px">
                  <el-button
                    type="danger"
                    size="mini"
                    icon="el-icon-delete"
                    circle
                    @click="delDateTime(index)"
                  ></el-button>
                </div>
              </el-col>
            </div>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="10"
              ><div>历时：{{ dateCountNumber }}天</div></el-col
            >
            <el-col :span="6"
              ><div>价格：{{ dateCountPrice }}元</div></el-col
            >
          </el-row>
        </div>
        <div class="infoTitle">
          <span
            >辅助设备<el-tooltip
              class="item"
              effect="dark"
              content="如不选择，则不提供以下设备"
              placement="top"
              style="margin-left: 5px"
            >
              <i class="el-icon-info"></i> </el-tooltip
          ></span>
          <!-- <span class="rightTxt">小计：<span class="redTxt">{{deviceCountNumber}}</span>元</span> -->
        </div>
        <div class="infoDetail">
          <el-row :gutter="20">
            <el-col :span="24"
              ><div>
                <el-form-item prop="val">
                  <el-checkbox-group
                    v-model="ruleForm.deviceList"
                    @change="deviceChange"
                  >
                    <el-checkbox
                      v-for="(item, index) in deviceList"
                      :key="index"
                      :label="item.id"
                      v-model="item.checked"
                      >{{ item.name }}</el-checkbox
                    >
                  </el-checkbox-group>
                </el-form-item>
              </div></el-col
            >
          </el-row>
        </div>
        <div class="infoTitle">
          <span
            >辅助服务<el-tooltip
              class="item"
              effect="dark"
              content="如不选择，则不提供以下服务"
              placement="top"
              style="margin-left: 5px"
            >
              <i class="el-icon-info"></i> </el-tooltip
          ></span>
          <span class="rightTxt"
            >小计：<span class="redTxt">{{ serviceCountNumber }}</span
            >元</span
          >
        </div>
        <div class="infoDetail">
          <el-row :gutter="20">
            <el-col :span="24"
              ><div>
                <el-form-item>
                  <el-checkbox-group
                    v-model="ruleForm.serviceList"
                    @change="serviceChange"
                  >
                    <span
                      v-for="(item, index) in serviceList"
                      :key="index"
                      class="serviceCheckBox"
                    >
                      <el-checkbox :value="item.checked" :label="item.id"
                        >{{ item.name }} {{ item.price / 100 }}元/{{
                          item.units
                        }}</el-checkbox
                      >
                      <el-input-number
                        v-model="item.number"
                        :controls="false"
                        placeholder="数量"
                        style="width: 60px; margin: 0px 10px"
                        v-show="item.checked"
                        @change="countService()"
                      ></el-input-number>
                    </span>
                  </el-checkbox-group>
                </el-form-item></div
            ></el-col>
          </el-row>
        </div>
        <div class="infoTitle">
          <span>会议室租赁保证金</span>
          <span class="rightTxt"
            >小计：<span class="redTxt">{{ earnestMoney }}</span
            >元</span
          >
        </div>
        <div class="infoDetail">
          <el-row :gutter="20">
            <el-col :span="6" :offset="18">
              <div>
                <el-form-item label="支付方式：">
                  <el-radio-group v-model="ruleForm.val">
                    <el-radio label="1">全款支付</el-radio>
                    <!--                    <el-radio label="2">分期支付</el-radio>-->
                  </el-radio-group>
                </el-form-item>
              </div>
            </el-col>
          </el-row>
        </div>
        <div class="countNum">
          <span>应付金额（元）：</span>
          <span class="bigTxt">{{allCountNumber}}</span>
        </div>

      </el-form>
    </div>
    <div class="submitBox">
      <el-button @click="back()">返回</el-button>
      <el-button type="danger" @click="submitForm()">确认提交</el-button>
    </div>
  </div>
</template>
<script>
import api from "../../../api/app";
export default {
  data() {
    return {
      id: 0,
      serviceCountNumber: 0,
      // deviceCountNumber: 0,
      earnestMoney: 0,
      dateCountPrice: 0,
      dateCountNumber: 0,
      companyData: {},
      adressData: {},
      fullMoney: 0,
      halfMoney: 0,
      timeArr: [
        {
          label: "上午09:00-12:00",
          value: 1,
          disabled: false,
        },
        {
          label: "下午14:00-18:00",
          value: 2,
          disabled: false,
        },
        {
          label: "全天09:00-18:00",
          value: 3,
          disabled: false,
        },
      ],
      meetingTimeList: [],
      facilityList: [],
      serveList: [],
      deviceList: [],
      serviceList: [],
      ruleForm: {
        val: "1",
        deviceList: [],
        serviceList: [],
        dateTimeArr: [
          {
            meetingDate: "",
            meetingTimeFrame: null,
            timeArr: [
              {
                label: "上午09:00-12:00",
                value: 1,
                disabled: false,
              },
              {
                label: "下午14:00-18:00",
                value: 2,
                disabled: false,
              },
              {
                label: "全天09:00-18:00",
                value: 3,
                disabled: false,
              },
            ],
          },
        ],
      },

      option: {
        disabledDate: (time) => {
          return time.getTime() < Date.now() + 2 * 24 * 3600 * 1000;
        },
      },
    };
  },
  computed:{
    allCountNumber(){
      return this.serviceCountNumber+this.earnestMoney+this.dateCountPrice
    }
  },
  created() {
    this.id = this.$route.query.id;
  },
  mounted() {
    this.findLeaseUserInfo();
    this.findMeetInfo();
  },
  methods: {
    back(){
      this.$router.push({path:'/meetingOrder'})
    },
    dateChange(item, idx) {
      console.log(item.meetingDate);
      console.log(idx);
      let data = {
        id: this.id,
        day: item.meetingDate,
      };
      api
        .findDayLeaseMeetInfo(data)
        .then((res) => {
          console.log(res);
          if (res.data.meetingTimeFrame == 1) {
            item.timeArr[0].disabled = true;
            item.timeArr[2].disabled = true;
          } else if (res.data.meetingTimeFrame == 2) {
            item.timeArr[1].disabled = true;
            item.timeArr[2].disabled = true;
          } else if (res.data.meetingTimeFrame == 3) {
            item.timeArr[0].disabled = true;
            item.timeArr[1].disabled = true;
            item.timeArr[2].disabled = true;
          }
          this.$set(this.ruleForm.dateTimeArr, idx, item)
        })
        .catch((err) => {console.log(err)});
    },
    submitForm() {
      if (
        this.getMeetingTimeList() &&
        this.getFacilityList() &&
        this.getServeList()
      ) {
        console.log("Pass");
        this.addPlmMeetOrder();
      }
    },
    getMeetingTimeList() {
      let status = true;
      let dateList = this.ruleForm.dateTimeArr;
      if (dateList.length == 0) {
        this.$message({
          message: "您还没有选择预定日期",
          type: "warning",
        });
        status = false;
      }
      for (let j = 0; j < dateList.length; j++) {
        if (dateList[j].meetingTimeFrame != null) {
          this.meetingTimeList.push(dateList[j]);
        } else {
          this.$message({
            message: "请确定日期和时段选择完整",
            type: "warning",
          });
          status = false;
        }
      }
      return status;
    },
    getFacilityList() {
      let deviceList = this.deviceList;
      console.log(deviceList);
      for (let j = 0; j < deviceList.length; j++) {
        if (deviceList[j].checked) {
          this.facilityList.push(deviceList[j]);
        }
      }
      return true;
    },
    getServeList() {
      let status = true;
      let serviceList = this.serviceList;
      for (let j = 0; j < serviceList.length; j++) {
        if (serviceList[j].checked) {
          if (serviceList[j].number > 0) {
            this.serveList.push(serviceList[j]);
          } else {
            this.$message({
              message: "请确定已勾选的服务所需数量不为0",
              type: "warning",
            });
            status = false;
          }
        }
      }
      return status;
    },
    addPlmMeetOrder() {
      let data = {
        companyCode: this.companyData.companyCode,
        companyId: this.companyData.companyId,
        companyName: this.companyData.companyName,
        legalMan: this.companyData.legalMan,
        legalManPhone: this.companyData.legalManPhone,
        linkMan: this.companyData.linkMan,
        linkPhone: this.companyData.linkPhone,
        plcMeetinRoomId: this.adressData.id,
        meetingTimeList: this.meetingTimeList,
        facilityList: this.facilityList,
        serveList: this.serveList,
      };
      api.addPlmMeetOrder(data).then(res=>{
        this.$router.push({
          path: '/payment',
          query: { id: res.data.orderNumber, money: res.data.amount / 100 }
        })
      }).catch(err=>{
        console.log(err)
      })
      console.log(data);
    },
    countDateInfo() {
      let dateList = this.ruleForm.dateTimeArr;
      var countNum = 0;
      let dateNum = 0;
      for (let j = 0; j < dateList.length; j++) {
        if (dateList[j].meetingTimeFrame == 1) {
          dateNum += 0.5;
          countNum += this.halfMoney;
        } else if (dateList[j].meetingTimeFrame == 2) {
          dateNum += 0.5;
          countNum += this.halfMoney;
        } else if (dateList[j].meetingTimeFrame == 3) {
          dateNum += 1;
          countNum += this.fullMoney;
        }
      }
      this.dateCountPrice = countNum;
      this.dateCountNumber = dateNum;
    },
    countService() {
      let serviceList = this.serviceList;
      var countNum = 0;
      for (let j = 0; j < serviceList.length; j++) {
        if (serviceList[j].checked) {
          countNum += (serviceList[j].price * serviceList[j].number) / 100;
        }
      }
      this.serviceCountNumber = countNum;
    },
    // countDevice() {
    //   let deviceList = this.deviceList;
    //   var countNum=0
    //   for (let j = 0; j < deviceList.length; j++) {
    //     if (deviceList[j].checked) {
    //       countNum+=deviceList[j].price/100
    //     }
    //   }
    //   this.deviceCountNumber=countNum
    // },
    serviceChange(val) {
      console.log(val);

      let tempList = val;
      let serviceList = this.serviceList;
      for (let j = 0; j < serviceList.length; j++) {
        console.log(tempList.includes(serviceList[j].id));
        if (tempList.includes(serviceList[j].id)) {
          serviceList[j].checked = true;
          this.$set(this.serviceList, j, serviceList[j]);
          this.countService();
        } else {
          serviceList[j].checked = false;
          this.$set(this.serviceList, j, serviceList[j]);
          this.countService();
        }
      }
      console.log(this.serviceList);
    },
    deviceChange(val) {
      console.log(val);
      let tempList = val;
      let deviceList = this.deviceList;
      for (let j = 0; j < deviceList.length; j++) {
        console.log(tempList.includes(deviceList[j].id));
        if (tempList.includes(deviceList[j].id)) {
          deviceList[j].checked = true;
          this.$set(this.deviceList, j, deviceList[j]);
        } else {
          deviceList[j].checked = false;
          this.$set(this.deviceList, j, deviceList[j]);
        }
      }
    },
    findLeaseUserInfo() {
      api
        .findLeaseUserInfo()
        .then((res) => {
          console.log(res);
          this.companyData = res.data.data;
        })
        .catch((err) => {console.log(err)});
    },
    findMeetInfo() {
      let data = { meetId: this.id };
      api
        .findMeetInfo(data)
        .then((res) => {
          console.log(res);
          this.adressData = res.data.data;
          this.fullMoney = res.data.data.fullMoney / 100;
          this.halfMoney = res.data.data.halfMoney / 100;
          this.earnestMoney = res.data.data.earnestMoney / 100;
          this.serviceList = [];
          for (let i = 0; i < res.data.data.meetServeList.length; i++) {
            let tempObj = res.data.data.meetServeList[i];
            tempObj.number = 0;
            tempObj.checked = false;
            this.serviceList.push(tempObj);
          }
          this.deviceList = [];
          for (let i = 0; i < res.data.data.meetFacilityList.length; i++) {
            let tempObj = res.data.data.meetFacilityList[i];
            tempObj.checked = false;
            this.deviceList.push(tempObj);
          }
        })
        .catch((err) => {console.log(err)});
    },
    addDateTime() {
      let temObj = {
        meetingDate: "",
        meetingTimeFrame: null,
        timeArr: [
          {
            label: "上午09:00-12:00",
            value: 1,
            disabled: false,
          },
          {
            label: "下午14:00-18:00",
            value: 2,
            disabled: false,
          },
          {
            label: "全天09:00-18:00",
            value: 3,
            disabled: false,
          },
        ],
      };
      this.ruleForm.dateTimeArr.push(temObj);
    },
    delDateTime(idx) {
      this.ruleForm.dateTimeArr.splice(idx, 1);
    },
  },
};
</script>
<style scoped>
.orderBox{
  margin-top: 50px;
  margin-bottom: 50px;
}
.pageTitle {
  text-align: center;
  margin-bottom: 10px;
  font-size: 20px;
}
.infoBox {
  width: 1068px;
  margin: 0 auto;
  border: 1px solid #cccccc;
  text-align: left;
}
.title {
  font-size: 20px;
  /* text-align: center; */
  line-height: 50px;
  padding: 0 20px;
  color: #66b1ff;
  font-weight: bold;
}
.infoTitle {
  padding: 0 20px;
  background: #c4c4c4;
  line-height: 40px;
  color: #262424;
}
.infoTitle span.rightTxt {
  float: right;
  font-size: 14px;
}
.infoTitle span.rightTxt .redTxt {
  color: #ff4c4c;
  padding: 0 5px;
}
.infoDetail {
  padding: 15px 20px;
  font-size: 14px;
  color: #666666;
  line-height: 30px;
}
.infoDetail >>> .el-input__inner {
  height: 30px;
  line-height: 30px;
}
.countNum {
  line-height: 50px;
  padding: 0 15px;
  background: #e7e6e6;
  color: red;
  text-align: right;
}
.countNum span.bigTxt {
  font-size: 1.1em;
}
.serviceCheckBox {
  margin-right: 30px;
}
.submitBox{
  width: 1068px;
  margin: 0 auto;
  text-align: right;
  margin-top: 50px;
}
</style>

